<template>
  <div class="edit-dialog">
    <el-dialog
      v-model="dialog"
      :title="titleName"
      :visible.sync="dialog"
      :close-on-click-modal="false"
      :show-close="true"
      width="500px"
      center
      @close="handleClose"
    >
      <el-form :model="addFormData" :rules="rules" ref="formRef" label-position="right" label-width="auto">
        <el-form-item prop="deviceId" label="设备ID" style="width: 100%">
          <el-input v-model="addFormData.deviceId" maxlength="80" size="medium" placeholder="请输入设备ID" style="width: 100%" />
        </el-form-item>
        <el-form-item label="设备名称" style="width: 100%">
          <el-input v-model="addFormData.name" maxlength="80" size="medium" placeholder="请输入设备名称" style="width: 100%" />
        </el-form-item>
        <el-form-item prop="type" label="设备类型" style="width: 100%">
          <el-radio-group v-model="addFormData.type" style="width: 100%">
            <el-radio label="IEQ">IEQ</el-radio>
            <el-radio label="PMS">PMS</el-radio>
            <el-radio label="OPT">OPT</el-radio>
            <el-radio label="ROBOT">ROBOT</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="设备地址" style="width: 100%">
          <el-input v-model="addFormData.address" size="medium" maxlength="80" placeholder="请输入设备地址" style="width: 100%" />
        </el-form-item>
        <el-form-item label="设备描述" style="width: 100%">
          <el-input type="textarea" v-model="addFormData.description" :rows="2" maxlength="80" placeholder="请输入设备描述" style="width: 100%" />
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" :loading="submitLoading" icon="el-icon-check" @click="submit('formRef')">提 交</el-button>
          <el-button  icon="el-icon-close" @click="handleClose">取 消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { create } from '@/api/deviceManage'
export default {
  name: 'deviceManage',
  props: {
    formData: Object,
    showDialog: Boolean,
    editType: Number
  },
  data() {
    return {
      addFormData: {
        type: 'IEQ'
      },
      titleName: '添加',
      submitLoading: false,
      rules: {
        deviceId: [{required: true, message: "请输入设备ID", trigger: "blur"}],
        type: [{required: true, message: "请选择设备类型", trigger: "blur"}]
      }
    }
  },
  computed: {
    dialog: function() {
      return this.showDialog
    }
  },
  methods: {
    // 取消事件
    handleClose() {
      this.$emit('changeVisible', false)
    },
    submit(formName) {
      this.submitLoading = true;
      this.$refs[formName].validate(valid => {
        if (valid) {
          const that = this
          const data = {
            ...that.addFormData
          }
          create(data)
            .then(res => {
              that.submitLoading = false
              if (res['code'] == 200) {
                that.$emit('changeVisible', false)
                that.$emit('getList')
                that.$message.success("添加成功!");
              } else {
                that.$message(res['msg'])
              }
            })
            .catch(() => {
              that.submitLoading = false
            })
        } else {
          this.submitLoading = false;
          return false;
        }
      });
    }
  }
}
</script>

<style>
</style>
